package com.myopen.render;

import com.jfinal.render.Render;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.net.URLEncoder;


public class PoiRender extends Render {

    private XSSFWorkbook wb;
    private String name;

    public XSSFWorkbook getWb() {
        return wb;
    }

    public void setWb(XSSFWorkbook wb) {
        this.wb = wb;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public PoiRender(XSSFWorkbook wb, String name) {
        this.wb = wb;
        this.name=name;
    }



    @Override
    public void render() {

        String filename="file.xlsx";

        try {


            String agent = request.getHeader("User-Agent").toUpperCase(); //获得浏览器信息并转换为大写
            if (agent.indexOf("MSIE") > 0 || (agent.indexOf("GECKO")>0 && agent.indexOf("RV:11")>0)) {  //IE浏览器和Edge浏览器
                filename=URLEncoder.encode(name,"UTF-8");
            } else {  //其他浏览器
                filename = new String(name.getBytes("UTF-8"), "iso-8859-1");  
            }
            
            response.addHeader("Content-disposition", "attachment; filename="+filename);
            //response.setContentType("application/x-msdownload");

            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
